﻿
#define _CRT_SECURE_NO_WARNINGS

#include <Windows.h>
#include <Commctrl.h>

//
#include "resource.h"
#include "FileList.h"

//
#define		WM_LIST_COMPLETE			(WM_APP+10101)

//
HINSTANCE g_hInstance = 0;
HWND g_hListViewFiles = 0;

//
INT_PTR WINAPI DlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

//
class ThreadParam
{
public:
	HWND hMainWnd;
};

//
DWORD g_dwListVolumeThread;
HANDLE g_hListVolume;
ThreadParam g_threadParam;

//
// 修改这个结构里的 szVolume 成员可以设置要遍历哪个磁盘， 默认是 D 盘
//
TParamList param; 

//
CListVolume listVolume;

//
DWORD ListVolumeProc(LPVOID lParam)
{
	listVolume.ListVolume(param);
	::PostMessageW(g_threadParam.hMainWnd, WM_LIST_COMPLETE, 0, 0);
	return 0;
}

//
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR szCmdLine, int iCmdShow)
{
	//
	g_hInstance = hInstance;

	::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)LogProc, 0, 0, &g_dwLogThread);

	Log("Program Start. Build: %s. Time: %d", __TIME__, Now());

	MSG msg;
	HWND hwnd = CreateDialogParamA(hInstance, MAKEINTRESOURCEA(IDD_DIALOG_MAIN), NULL, DlgProc, 0);
	DWORD dwError = ::GetLastError();
	ShowWindow(hwnd, SW_SHOW);
	while (GetMessageA(&msg, NULL, 0, 0))
	{
		if (!IsDialogMessageA(hwnd, &msg))
		{
			if (msg.message == WM_QUIT)
			{
				break;
			}
			else//
			{
				TranslateMessage(&msg);
				DispatchMessageA(&msg);
			}
		}
	}
	DestroyWindow(hwnd);
	return 0;
}

//
INT_PTR WINAPI DlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:
	{
		g_threadParam.hMainWnd = hwnd;

		//
		HWND listview = ::GetDlgItem(hwnd, IDC_LIST_FILES);

		LVCOLUMNW lvc;
		lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
		lvc.pszText = L"文件名";
		lvc.cx = 300;
		lvc.iImage = 1;
		lvc.fmt = LVCFMT_LEFT;
		lvc.iSubItem = 0;
		ListView_InsertColumn(listview, 0, &lvc);

		lvc.pszText = L"路径";
		lvc.cx = 600;
		lvc.iSubItem = 1;
		ListView_InsertColumn(listview, 1, &lvc);

		// 创建遍历线程，
		// 遍历完成会发送 WM_LIST_COMPLETE 消息，
		// 将结果显示在列表
		::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ListVolumeProc, 0, 0, &g_dwListVolumeThread);
	}
	break;
	case WM_CLOSE:
	{
		PostQuitMessage(0);
		return ::DefWindowProcA(hwnd, message, wParam, lParam);
	}
	case WM_PAINT:
	{
		PAINTSTRUCT ps;
		HDC hdc = BeginPaint(hwnd, &ps);
		EndPaint(hwnd, &ps);
		break;
	}
	case WM_LIST_COMPLETE:
	{
		UInt nFileCounts = listVolume.m_vecFolderInfo.size() + listVolume.m_vecFileInfo.size();
		HWND listview = ::GetDlgItem(hwnd, IDC_LIST_FILES);
		ListView_SetItemCountEx(listview, nFileCounts, LVSICF_NOINVALIDATEALL | LVSICF_NOSCROLL);
		break;
	}
	case WM_NOTIFY:
	{
		switch (((LPNMHDR)lParam)->code)
		{
		case LVN_GETDISPINFOA:
		{
			UInt nFileCounts = listVolume.m_vecFolderInfo.size() + listVolume.m_vecFileInfo.size();

			NMLVDISPINFOA *pDispInfo = reinterpret_cast<NMLVDISPINFOA*>((LPNMHDR)lParam);
			LVITEMA* lvi = &(pDispInfo)->item;
			lvi->mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM | LVIF_STATE;
			lvi->state = 0;
			lvi->stateMask = 0;
			if (lvi->iSubItem == 0 && lvi->iItem < nFileCounts)
			{
				char* log = NULL;
				if (lvi->iItem < listVolume.m_vecFolderInfo.size())
				{
					TFileInfo* fileInfo = listVolume.m_vecFolderInfo[lvi->iItem];
					lvi->pszText = fileInfo->szName;
				}
				else if (lvi->iItem < nFileCounts)
				{
					TFileInfo* fileInfo = listVolume.m_vecFileInfo[lvi->iItem - listVolume.m_vecFolderInfo.size()];
					lvi->pszText = fileInfo->szName;
				}

				if(lvi->pszText)
					lvi->cchTextMax = strlen(lvi->pszText) + 1;
			}
			else if (lvi->iSubItem == 1 && lvi->iItem < nFileCounts)
			{
				if (lvi->iItem < listVolume.m_vecFolderInfo.size())
				{
					TFileInfo* fileInfo = listVolume.m_vecFolderInfo[lvi->iItem];
					CListVolume::FileInfoMap::iterator it = listVolume.m_mapFolderInfo.find(fileInfo->iParentFileReferenceNumber);
					if (it != listVolume.m_mapFolderInfo.end())
					{
						lvi->pszText = it->second->at(0)->szPath;
					}
				}
				else if (lvi->iItem < nFileCounts)
				{
					TFileInfo* fileInfo = listVolume.m_vecFileInfo[lvi->iItem - listVolume.m_vecFolderInfo.size()];
					CListVolume::FileInfoMap::iterator it = listVolume.m_mapFolderInfo.find(fileInfo->iParentFileReferenceNumber);
					if (it != listVolume.m_mapFolderInfo.end())
					{
						lvi->pszText = it->second->at(0)->szPath;
					}
				}
				if (lvi->pszText)
					lvi->cchTextMax = strlen(lvi->pszText) + 1;
			}
			return 0;
		}
		break;
		default:
			break;
		}
		break;
	}
	case WM_COMMAND:
	{
		switch (LOWORD(wParam))
		{
		case IDCANCEL:
			PostQuitMessage(0);
			return 1;
		}
	}
	break;
	}
	return ::DefWindowProcA(hwnd, message, wParam, lParam);
}
